<template>
	<view class="page Signin">
		<image class="Signin_imgchoice" src="https://static.newbeaconhotels.com/app/hotel/my/bg-login.png"></image>
		<view class="Signin_bos">
			<view class="Signin_hello">你好</view>
			<view class="Signin_choice">请选择登录方式</view>
			<view class="Signin_box">
				<image class="logo" src="https://static.newbeaconhotels.com/app/hotel/my/logo.png"></image>
				<view class="font15 font-323232 font-b">纽宾凯酒店官方预订平台</view>
				<!--  #ifdef  MP-WEIXIN -->
				<button class="btn btn1 flex-row-center" @click="getUserProfile">一键登录</button>
				<!--  #endif -->

				<!--  #ifdef  H5 -->
				<button class="btn btn1 flex-row-center" @click="h5Login">一键登录</button>
				<!--  #endif -->

				<view class="btn btn2 flex-row-center" @click="goNext('/pages/common/loginmp')">使用手机号码登录</view>
				
				<view class="tips font-969696" @click="goNext('/pages/common/update?mode=create')">还未注册？<text class="font-link">去注册</text></view>
			</view>
		</view>
		<!-- 登录弹窗 -->
		<uni-popup ref="popup">
			<view class="popup_bos flex-column-between">
				<view class="font14 font-1e1e1e">登录授权 申请</view>
				<view class="popup_box flex-row-start">
					<image :src="wxUser.headimgurl" mode=""></image>
					<view class="font14 font-1e1e1e">{{wxUser.nickname}}</view>
				</view>
				<view class="popup_button flex-row-around">
					<view class="flex-row-center font12" @click="Onclose">拒绝</view>
					<button class="flex-row-center font12 font-ffffff" open-type="getPhoneNumber"
						@getphonenumber="getPhoneNumber">同意</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wxcode: '', //微信code
				// 授权返回
				wxUser: {
					nickname: '',
					headimgurl: ''
				},
				idInfo: {}
			}
		},
		onLoad: function(options) {
			// 从缓存中拿取各种id信息
			this.idInfo = uni.getStorageSync('idInfo')
		},
		onShow: function() {
			// #ifdef MP-WEIXIN
			wx.login({
				success: res => {
					this.wxcode = res.code
				}
			})
			// #endif
		},
		methods: {
			// 小程序获取头像昵称
			getUserProfile() {
				var that = this
				let click = true
				if (click) {
					click = false
					wx.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							that.wxUser.nickname = res.userInfo.nickName
							that.wxUser.headimgurl = res.userInfo.avatarUrl
							that.$refs.popup.open()
							click = true
						},
						fail: (res) => {
							click = true
							//不同意不做任何处理
						}
					})
				}

			},
			// 微信一键登录
			getPhoneNumber(e) {
				var that = this
				let click = true
				that.$refs.popup.close();
				if(click){
					click = false
					//用户通过
					if (e.detail.errMsg == 'getPhoneNumber:ok') {
						that.$Http({
							url: 'login',
							type: 'POST',
							load: true,
							data: {
								code: that.wxcode,
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv,
								nickname: that.wxUser.nickname,
								avatar: that.wxUser.headimgurl,
								group: 'wxapp'
							}
						}).then(res => {
							click = true
							// 重新获取code
							wx.login({
								success: res1 => {
									that.wxcode = res1.code
								}
							})
							if (res.code == 1) {
								uni.setStorage({
									key: 'token',
									data: res.data.token,
									success() {
										// 登录完成后获取用户信息
										that.getUserInfo()
									}
								})
							} else if (res.code == 10) {
								uni.showModal({
									title: '提示',
									confirmText: '确定',
									confirmColor: '#03004c',
									cancelText: '取消',
									content: '您还未注册，请先注册',
									success: function(res1) {
										if (res1.confirm) {
											uni.navigateTo({
												url: '/pages/common/update?mode=create'
											});
										}
									}
								});
							} else if (res.code == 500) {
								uni.showToast({
									title: '接口异常，请稍后再试',
									icon: 'none'
								});
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								});
							}
						});
					} else {
						that.$refs.popup.open()
						click = true
					}
				}

			
			},
			// H5一键登录
			h5Login() {
				var that = this
				that.$Http({
					url: 'login',
					type: 'POST',
					load: true,
					data: {
						group: 'wechat',
						openid: uni.getStorageSync('openid')
					}
				}).then(res => {
					if (res.code == 1) {
						uni.setStorage({
							key: 'token',
							data: res.data.token,
							success() {
								// 登录完成后获取用户信息
								that.getUserInfo()
							}
						})
					} else if (res.code == 10) {
						uni.showModal({
							title: '提示',
							confirmText: '确定',
							confirmColor: '#03004c',
							cancelText: '取消',
							content: '您还未注册，请先注册',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({ url: '/pages/common/update?mode=create' });
								}
							}
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			},
			// 获取用户信息
			getUserInfo() {
				this.$Http({
					url: 'user',
					token: true,
					load: true
				}).then(res => {
					if (res.code == 1) {
						let obj = {};
						// 员工分享的父id是自己，会员分享的父id是终极父id
						let type = res.data.user.group
						if (type.indexOf('员工') >= 0) {
							obj.Pid = res.data.user.user_id
						} else if (type.indexOf('会员') >= 0) {
							obj.Pid = res.data.user.parent_id
						}

						// 分享出去的来源id永远是自己的id
						obj.Fid = res.data.user.user_id;

						let $C = this.idInfo.Cid;
						if ($C) {
							obj.Cid = $C
						}

						uni.setStorage({
							key: 'idInfo',
							data: obj
						})
						uni.setStorage({
							key: 'userInfo',
							data: res.data
						})

						this.$base.Onpageurl()
					}
				});
			},
			// 关闭弹窗
			Onclose() {
				this.$refs.popup.close()
			},
			// 路由跳转
			goNext(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
